package com.example.windowmanager;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.example.windowmanager.service.WindowService;
import com.example.windowmanager.utils.OnPermissionResponseListener;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.rmondjone.camera.CameraActivity;
import com.rmondjone.camera.CameraPreview;
import com.rmondjone.camera.OverCameraView;
import com.rmondjone.camera.PermissionUtils;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

public class MainActivity extends BaseActivity {
    private Button btn_start;
    private Button btn_end;
    private Button btn_carmer;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }


    @Override
    public void initView() {
        btn_carmer = findViewById(R.id.btn_carmer);
        btn_start = findViewById(R.id.btn_start);
        btn_end = findViewById(R.id.btn_end);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(view -> Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show());

    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

        btn_carmer.setOnClickListener(view -> {
            startMe(me, 2005, CameraActivity.MongolianLayerType.IDCARD_POSITIVE);
        });
        btn_start.setOnClickListener(view -> {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
//没有权限，需要申请权限，因为是打开一个授权页面，所以拿不到返回状态的，所以建议是在onResume方法中从新执行一次校验
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                intent.setData(Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, 100);
            } else {
                //已经有权限，可以直接显示悬浮窗
                requestPermission(new String[]{
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA,

                }, new OnPermissionResponseListener() {
                    @Override
                    public void onSuccess(String[] permissions) {
                        Intent intent = new Intent(MainActivity.this, WindowService.class);
                        startService(intent);
                    }

                    public void onFail() {
                        Toast.makeText(me, "获取权限失败", Toast.LENGTH_SHORT).show();
                    }
                });

            }

        });
        btn_end.setOnClickListener(view -> {
            Intent intent1 = new Intent(MainActivity.this, WindowService.class);
            stopService(intent1);
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    /**
     * 启动拍照界面
     *
     * @param activity
     * @param requestCode
     * @param type
     */
    public static void startMe(Activity activity, int requestCode, CameraActivity.MongolianLayerType type) {
        PermissionUtils.applicationPermissions(activity, new PermissionUtils.PermissionListener() {
            @Override
            public void onSuccess(Context context) {
                Intent intent = new Intent(activity, MainActivity.class);
                intent.putExtra("MongolianLayerType", type);
                activity.startActivityForResult(intent, requestCode);
            }

            @Override
            public void onFailed(Context context) {
                if (AndPermission.hasAlwaysDeniedPermission(context, Permission.Group.CAMERA)
                        && AndPermission.hasAlwaysDeniedPermission(context, Permission.Group.STORAGE)) {
                    AndPermission.with(context).runtime().setting().start();
                }
                Toast.makeText(context, context.getString(com.rmondjone.camera.R.string.permission_camra_storage), Toast.LENGTH_SHORT);
            }
        }, Permission.Group.STORAGE, Permission.Group.CAMERA);
    }
}